function MakePPTfromXLS( sDir)
%MakePPTfromXLS creates a presentation out of the statistics from Excel
%files
%sDir should be the parent directory that will be searched
%sPPT is the name (and path) of the output file
    function aiXYReturn = SearchStage(cellData)
        %Search for 'Stage:' in cell array
        if (strcmp(cellData{10,1},'Stage:'))
            aiXYReturn = [10,1];
            return
        end;
        for iRow=1:size(cellData,1)
            for iCol=1:size(cellData,2)
                if (strcmp(cellData{iRow,iCol},'Stage:'))
                    aiXYReturn = [iRow, iCol];
                    return
                end;
            end;
        end;
    end

    function aiXYReturn = SearchComment(cellData)
        %Search for 'Stage:' in cell array
        if (strcmp(cellData{11,1},'Stage:'))
            aiXYReturn = [11,1];
            return
        end;
        for iRow=1:size(cellData,1)
            for iCol=1:size(cellData,2)
                if (strcmp(cellData{iRow,iCol},'Comment:'))
                    aiXYReturn = [iRow, iCol];
                    return
                end;
            end;
        end;
        %Some old files have no comment place present
        aiXYReturn = [11,1];
    end
    
    function dsReturn = Crawler (sDir)
        %Crawl through all files and sub dirs
        lList  = dir(sDir);
        dsReturn=dataset();
        for iFile = 1:length(lList)
            sFilename = lList(iFile).name;
            if (lList(iFile).isdir)
                if (~strcmp(sFilename,'.') && ~strcmp(sFilename,'..'))
                    dsReturn=vertcat(dsReturn,Crawler([sDir '\' sFilename]));
                end;
            %Is it a valid excel file?, xlsfinfo has problems with some
            %files, so check the extension first
            elseif (strcmp('.xls',sFilename(end-3:end)) && ~strcmp(xlsfinfo([sDir '\' sFilename]),''))
                %Check if it is the newest version
                sSearchString = regexprep(sFilename,'\d{6}','*');
                lTempList = dir([sDir '\' sSearchString]);
                if (~strcmp(lTempList(end).name,sFilename))
                    continue;
                end;
                %Read data from the Excel file
                try
                    [~,~,cellData] = xlsread([sDir '\' sFilename],'Data');
                    [~,~,cellHist] = xlsread([sDir '\' sFilename],'Histogram');
                catch
                    disp(['Strange Excel sheet couldn''t be opened ' sDir '\' sFilename]);
                    continue;
                end;
                %Try to guess the Genotype from the filname
                if     (~isempty(regexp(sFilename,'RA-?IIF','once')) || ~isempty(regexp(sFilename,'^IIF','once')))
                    sGenotype = 'RA-IIF';
                elseif (~isempty(regexp(sFilename,'RA-?YC' ,'once')) || ~isempty(regexp(sFilename,'^YC' ,'once')))
                    sGenotype = 'RA-YC';
                elseif (~isempty(regexp(sDir,'RA-?IIF','once')))
                    sGenotype = 'RA-IIF';
                elseif (~isempty(regexp(sDir,'RA-?YC','once')))
                    sGenotype = 'RA-YC';
                elseif (~isempty(strfind(sFilename,'EGFPnos')))
                    sGenotype = 'EGFPnos';
                elseif (~isempty(strfind(sDir,'EGFPnos')))
                    sGenotype = 'EGFPnos';
                else
                    sGenotype = 'unknown';
                end;
                
                %Try to guess whether it is 1X or 2X from the filename
                if     ~isempty(regexpi(sFilename,'1X'))
                    iCopies = 1;
                elseif ~isempty(regexpi(sFilename,'2X'))
                    iCopies = 2;
                elseif ~isempty(regexpi(sDir,'1X'))
                    iCopies = 1;
                else
                    iCopies = 2;
                end;
                
                %Make new string of fixed length out of Genotype and number
                %of copies
                sGenotype = sprintf('%-10s',[sGenotype ' ' num2str(iCopies,'%1.0f') 'X']);
                
                aiStage  = SearchStage(cellData)+ [0,1];
                aiComment= SearchComment(cellData) + [0,1];
                iVolOoc = cellData{2,4};
                iVolNur = cellData{3,4};
                dVolOoc = cellData{2,6};
                dVolNur = cellData{3,6};
                dIntOocToWhole = cellData{4,4};
                iIntOoc = cellData{2,5};
                iIntNur = cellData{3,5};
                dIntDiffOocNur = iIntOoc / iVolOoc - iIntNur / iVolNur;
                
                struHist.head   = cellHist(1,:);
                struHist.data   = cellHist(2:end,:);
                struHist.thumb  = [sDir '\' sFilename(1:end-3) 'thumb.tif'];
                struHist.file   = [sDir '\' sFilename];
                struHist.comment= cellData{aiComment(1,1),aiComment(1,2)};
                %Datasets can't cope with different variable types, so
                %convert all stages to strings of same length
                if (isnumeric(cellData{aiStage(1,1),aiStage(1,2)}))
                    sStage = sprintf('%-40s',num2str(cellData{aiStage(1,1),aiStage(1,2)}));
                else
                    sStage = sprintf('%-40s',strtrim(cellData{aiStage(1,1),aiStage(1,2)}));
                end; 
                sStage=sStage(1:40);
             
                
                dsTemp  = dataset({sGenotype,'Genotype'}, {sStage,'Stage'} ,{dVolOoc,'VolOoc'}, {dVolNur,'VolNur'},...
                    {iVolOoc/(iVolOoc+iVolNur),'VolOocToWhole'}, {dIntOocToWhole,'IntOocToWhole'} , {struHist,'Histogram'}, ...
                    {iIntOoc/iVolOoc, 'AvIntOoc'}, {iIntNur/iVolNur, 'AvIntNur'}, {dIntDiffOocNur, 'DiffIntOocNur'});
                dsReturn= vertcat(dsReturn, dsTemp);
            end;
        end;
        return;
    end

    
    %Check if sDir is present if not let user choose Directory
    if (~exist('sDir','var'))
        sDir = uigetdir('C:\','Choose a directory that should be parsed for the Powerpoint presentation');
        if (isequal(sDir,0))
            return;
        end;
    end;
    if (~isdir(sDir))
        return;
    end;
    dsData = Crawler(sDir);
    %Don't continue if no files were found
    if isempty(dsData)
        return;
    end;
    [~,aiIndex]=sortrows([single(dsData(:,'Genotype')) single(dsData(:,'Stage'))]);
    dsData=dsData(aiIndex,:);
    
    %Create summary for every stage and create the Excel file
    %For summary
    sLastStage='';
    iEntries=0;
    dsSummary = dataset();
    %For Excel worksheet
    apExcel = actxserver('Excel.Application');
    apExcel.Workbooks.Add;
    apExcel.Worksheets.Item(1).Name='Data';
    apExcel.Range('A1').FormulaR1C1 = 'Genotype';
    apExcel.Range('B1').FormulaR1C1 = 'Stage';
    apExcel.Range('C1').FormulaR1C1 = 'Stage as number';
    apExcel.Range('D1').FormulaR1C1 = 'Volume of whole compound';
    apExcel.Range('E1').FormulaR1C1 = 'Volume of oocyte';
    apExcel.Range('F1').FormulaR1C1 = 'Ratio: Volume oocyte to whole volume';
    apExcel.Range('G1').FormulaR1C1 = 'Average intensity in oocyte';
    apExcel.Range('H1').FormulaR1C1 = 'Average intensity in nurse cells';
    apExcel.Range('I1').FormulaR1C1 = 'Difference of mean intensity oocyte to nursecells';
    apExcel.Range('J1').FormulaR1C1 = 'Ratio: Mean intensity ooccyte to whole';
    apExcel.Range('K1').FormulaR1C1 = 'Most common intensity oocyte';
    apExcel.Range('L1').FormulaR1C1 = 'Most common intensity nurse cells';
    apExcel.Range('M1').FormulaR1C1 = 'Ratio: Most common intensity oocyte to nurse cells';
    apExcel.Range('N1').FormulaR1C1 = 'Difference: Most common intensity oocyte to nurse cells';
    apExcel.Range('O1').FormulaR1C1 = 'Filename';
    apExcel.Range('A2').Activate;
    %Do it for every entry in our dataset
    for iDS = 1:length(dsData)
        %Was the last entry the last entry for this stage?
        %Then delete the vectors for mean and stdev!
        if (~strcmp(sLastStage,[dsData{iDS,'Genotype'} dsData{iDS,'Stage'}]))
            if (iEntries~=0)
                dsTemp = dataset({dsData{iDS-1,'Genotype'},'Genotype'}, {dsData{iDS-1,'Stage'},'Stage'},{struData,'Data'});
                dsSummary=vertcat(dsSummary,dsTemp);
            end;
            struData.adVO2W  =[];
            struData.adIO2W  =[];
            struData.adIOMinusN = [];
            struData.adVW    =[];
            struData.adVO    =[];
            struData.adMaxO2N=[];
            struData.adMaxOMinusN=[];
            iEntries=0;
            sLastStage=[dsData{iDS,'Genotype'} dsData{iDS,'Stage'}];
        end;
        
        %Add genotype & stage to Excel
        apExcel.ActiveCell.FormulaR1C1 = strtrim(dsData{iDS,'Genotype'});
        strStage=strtrim(dsData{iDS,'Stage'});
        apExcel.ActiveCell.Range('B1').FormulaR1C1 = ['="' strStage '"'];
        %Add stage as number (that requires a bit of parsing
        if ~isnan(str2double(strStage))
            dStage = str2double(strStage);
        elseif ~isempty(regexp(strStage,'\d+ or \d+','ONCE'))
            strStage = regexp(strStage,'\d+ or \d+','ONCE','match');
            celldStage = regexp(strStage,'\d+','match');
            dStage = (str2double(celldStage{1})+str2double(celldStage{2}))/2;
        elseif (length(regexp(strStage,'\d+'))==1)
            strStage = regexp(strStage,'\d+','ONCE','match');
            dStage = str2double(strStage);
        else
            dStage = 0;
        end;
        apExcel.ActiveCell.Range('C1').FormulaR1C1 = dStage;
        %Add volumes and intensities to statistics
        struData.adVO2W=[struData.adVO2W, dsData.VolOocToWhole(iDS) ];
        struData.adIO2W=[struData.adIO2W, dsData.IntOocToWhole(iDS) ];
        struData.adVW  =[struData.adVW  , dsData.VolOoc(iDS) + dsData.VolNur(iDS)];
        struData.adVO  =[struData.adVO  , dsData.VolOoc(iDS)];
        struData.adIOMinusN = [struData.adIOMinusN, dsData.DiffIntOocNur(iDS)];
        apExcel.ActiveCell.Range('D1').FormulaR1C1 = dsData.VolOoc(iDS) + dsData.VolNur(iDS);
        apExcel.ActiveCell.Range('E1').FormulaR1C1 = dsData.VolOoc(iDS);
        apExcel.ActiveCell.Range('F1').FormulaR1C1 = dsData.VolOocToWhole(iDS);
        apExcel.ActiveCell.Range('G1').FormulaR1C1 = dsData.AvIntOoc(iDS);
        apExcel.ActiveCell.Range('H1').FormulaR1C1 = dsData.AvIntNur(iDS);
        apExcel.ActiveCell.Range('I1').FormulaR1C1 = dsData.DiffIntOocNur(iDS);
        apExcel.ActiveCell.Range('J1').FormulaR1C1 = dsData.IntOocToWhole(iDS);
        
        %Look at maximum instead of mean for Oocyte
        [~,aiIndex]=max(cell2mat(dsData{iDS,'Histogram'}.data(1:end,2)));
        %Fit maximum to get a better approximation for that we take a
        %region around the maximum
        %iStart=aiIndex(1)-3;
        %iEnd  =aiIndex(1)+3;
        %adX = cell2mat(dsData{iDS,'Histogram'}.data(iStart:iEnd,1));
        %adY = cell2mat(dsData{iDS,'Histogram'}.data(iStart:iEnd,2));
        %adFit = polyfit(adX,adY,2);
        %dMaxIntOoc = -adFit(2)/2/adFit(1);
        adX = cell2mat(dsData{iDS,'Histogram'}.data(aiIndex-3:aiIndex+3,1));
        adY = cell2mat(dsData{iDS,'Histogram'}.data(aiIndex-3:aiIndex+3,2));
        dMaxIntOoc = sum(adX.*adY)/sum(adY);
       
        
        %For Nurse cells
        [~,aiIndex]=max(cell2mat(dsData{iDS,'Histogram'}.data(2:end,3)));
        %iStart=aiIndex(1)-3;
        %iEnd  =aiIndex(1)+3;
        %adX = cell2mat(dsData{iDS,'Histogram'}.data(iStart:iEnd,1));
        %adY = cell2mat(dsData{iDS,'Histogram'}.data(iStart:iEnd,3));
        %adFit = polyfit(adX,adY,2);
        %dMaxIntNur = -adFit(2)/2/adFit(1);
        adX = cell2mat(dsData{iDS,'Histogram'}.data(aiIndex-3:aiIndex+3,1));
        adY = cell2mat(dsData{iDS,'Histogram'}.data(aiIndex-3:aiIndex+3,3));
        dMaxIntNur = sum(adX.*adY)/sum(adY);
        
        %Calculate Ratio and Subtract values for more info
        struData.adMaxO2N=[struData.adMaxO2N, dMaxIntOoc/dMaxIntNur];
        struData.adMaxOMinusN=[struData.adMaxOMinusN, dMaxIntOoc - dMaxIntNur];
        apExcel.ActiveCell.Range('K1').FormulaR1C1 = dMaxIntOoc;
        apExcel.ActiveCell.Range('L1').FormulaR1C1 = dMaxIntNur;
        apExcel.ActiveCell.Range('M1').FormulaR1C1 = dMaxIntOoc/dMaxIntNur;
        apExcel.ActiveCell.Range('N1').FormulaR1C1 = dMaxIntOoc - dMaxIntNur;
        apExcel.ActiveCell.Range('O1').FormulaR1C1 = dsData{iDS,'Histogram'}.file;
        
        iEntries=iEntries+1;
        apExcel.ActiveCell.Range('A2').Activate;
    end;
    
    dsTemp = dataset({dsData{iDS,'Genotype'},'Genotype'}, {dsData{iDS,'Stage'},'Stage'},{struData,'Data'});
    dsSummary=vertcat(dsSummary,dsTemp);
    %Set number fomat to scientific
    apExcel.Cells.NumberFormat = '0.000E+0';
    %Set column width automatically
    apExcel.Columns.AutoFit;
    %Apply filter
    apExcel.Columns.AutoFilter;
    apExcel.Range('A1:O1').Activate;
    apExcel.ActiveWindow.SplitColumn = 0;
    apExcel.ActiveWindow.SplitRow = 1;
    apExcel.ActiveWindow.FreezePanes = true;
    
    
    %%
    %Okay, now that we have the summary let's do an output
    apPPT = actxserver('PowerPoint.Application');
    Presentation = apPPT.Presentations.Add;
    
    %Create a temporary file
    sTempImage = [tempname '.jpeg'];
    %Create a summary for every Genotype
    sOldGeno='';
    for iPhenoStage = 1:length(dsSummary)
        if (~strcmp(sOldGeno,dsSummary{iPhenoStage,'Genotype'}))
            sOldGeno=dsSummary{iPhenoStage,'Genotype'};
            % Add a new slide (with title object):
            NewSlide = Presentation.Slides.invoke('Add',Presentation.Slides.Count+1,11);
            NewSlide.Shapes.Title.TextFrame.TextRange.Text=['Summary for ' sOldGeno];
            %How long should the table be?
            iEntries=0;
            while strcmp(sOldGeno,dsSummary{iEntries+iPhenoStage,'Genotype'})
                iEntries = iEntries +1;
                if (iEntries + iPhenoStage > length(dsSummary))
                    break;
                end;
            end;
            ShapeTable = NewSlide.Shapes.AddTable(iEntries+1,9);
            iTableRow=2;

            %Make the table bigger
            ShapeTable.Left  = 0;
            ShapeTable.Width = Presentation.PageSetup.SlideWidth; 
            %Set table header
            ShapeTable.Table.Cell(1,1).Shape.TextFrame.TextRange.Text='Genotype';
            ShapeTable.Table.Cell(1,2).Shape.TextFrame.TextRange.Text='Stage';
            ShapeTable.Table.Cell(1,3).Shape.TextFrame.TextRange.Text='Mean volume Ooc/whole';
            ShapeTable.Table.Cell(1,4).Shape.TextFrame.TextRange.Text='STD';
            ShapeTable.Table.Cell(1,5).Shape.TextFrame.TextRange.Text='Mean volume';
            ShapeTable.Table.Cell(1,6).Shape.TextFrame.TextRange.Text='STD';
            ShapeTable.Table.Cell(1,7).Shape.TextFrame.TextRange.Text='Mean intensity Ooc/whole';
            ShapeTable.Table.Cell(1,8).Shape.TextFrame.TextRange.Text='STD';
            ShapeTable.Table.Cell(1,9).Shape.TextFrame.TextRange.Text='Entries';
            
            
            %We add more slides with the figures for our data and put it
            %into the Excel sheet
            
            %Add a new Excel worksheet
            apExcel.Worksheets.Add(apExcel.Worksheets.Item(2));
            apExcel.Worksheets.Item(2).Name = sOldGeno;
            apExcel.Range('A1').FormulaR1C1 = 'Stage';
            
            asFieldNames=fieldnames(dsSummary{iPhenoStage,'Data'});
            for iDataField=1:length(asFieldNames)
                cellStage=[];
                adValues=[];
                adMean  =[];
                adStD   =[];
                aiCodedStage=[];
                for iStage = 1:iEntries
                    adTempValues=getfield(dsSummary{iPhenoStage-1+iStage,'Data'},asFieldNames{iDataField,1});
                    %We have to code the stages as integers for the plot
                    %since matlab can't plot doubles against strings
                    aiCodedStage= [aiCodedStage; ones(length(adTempValues),1)*iStage];
                    adValues    = [adValues; adTempValues'];
                    adMean      = [adMean; mean(adTempValues)];
                    adStD       = [adStD; std(adTempValues)];
                    cellStage   = vertcat(cellStage, cellstr(strtrim(dsSummary{iPhenoStage-1+iStage,'Stage'})));
                end;
                %Plot data
                figure('Visible','Off');
                plot(aiCodedStage,adValues,'rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10);
                %Set windows size
                set(gcf,'Position',[0,500,3*(Presentation.PageSetup.SlideWidth-20), 3*(Presentation.PageSetup.SlideHeight-100)]);
                %Set ticks and label them
                set(gca,'XTick',1:iEntries);
                set(gca,'XTickLabel',cellStage);
                %The limits of the axes should be a bit greater
                set(gca,'XLim',[0, iEntries+1]);
                %Save file and add to new slide
                saveas(gcf,sTempImage,'jpeg');
                NewSlide = Presentation.Slides.invoke('Add',Presentation.Slides.Count+1,11);
                %Create title
                switch asFieldNames{iDataField,1}
                    case 'adVO2W'
                        sObservable = 'Volume Oocyte/Whole';
                    case 'adIO2W'
                        sObservable = 'Intensity Oocyte/Whole';
                    case 'adVW'
                        sObservable = 'Volume of the whole compound';
                    case 'adVO'
                        sObservable = 'Volume of oocyte';
                    case 'adMaxO2N'
                        sObservable = 'Ratio: Most common intensity oocyte to nursecells';
                    case 'adMaxOMinusN'
                        sObservable = 'Difference: Most common intensity oocyte minus nursecells';
                    case 'adIOMinusN'
                        sObservable = 'Difference: Mean intensity oocyte minus nursecells';
                    otherwise
                        sObservable = asFieldNames{iDataField,1};
                end;
                NewSlide.Shapes.Title.TextFrame.TextRange.Text=['Figures for ' sOldGeno ', ' sObservable];
                NewSlide.Shapes.AddPicture(sTempImage,false, true, 10,100, Presentation.PageSetup.SlideWidth-20, Presentation.PageSetup.SlideHeight-100 );
                %Close figure again
                close;
                
                %Add a summary for every stage
                apExcel.Range(['A2:A' num2str(1+length(cellStage))]).FormulaR1C1=cellStage;
                apExcel.ActiveCell.Range('B1').FormulaR1C1 = sObservable;
                apExcel.ActiveCell.Range(['B2:B' num2str(1+length(cellStage))]).FormulaR1C1 = adMean;
                apExcel.ActiveCell.Range('C1').FormulaR1C1 = '+/-';
                apExcel.ActiveCell.Range(['C2:C' num2str(1+length(cellStage))]).FormulaR1C1 = adStD;
                apExcel.ActiveCell.Range('C1').Activate;
            end;
            
            
            
        end;
            
        %don't show std if there is only one entry
        if (1~=length(dsSummary{iPhenoStage,'Data'}.adIO2W));
            sTempVol   = sprintf('%1.0f',std(dsSummary{iPhenoStage,'Data'}.adVW  ));
            sTempInt   = sprintf('%1.3f',std(dsSummary{iPhenoStage,'Data'}.adIO2W));
            sTempVolRat= sprintf('%1.4f',std(dsSummary{iPhenoStage,'Data'}.adVO2W));
        else
            sTempVol='';
            sTempInt='';
            sTempVolRat='';
        end;
        %Write table
        ShapeTable.Table.Cell(iTableRow,1).Shape.TextFrame.TextRange.Text=strtrim(dsSummary{iPhenoStage,'Genotype'});
        ShapeTable.Table.Cell(iTableRow,2).Shape.TextFrame.TextRange.Text=strtrim(dsSummary{iPhenoStage,'Stage'});
        ShapeTable.Table.Cell(iTableRow,3).Shape.TextFrame.TextRange.Text=sprintf('%1.4f',mean(dsSummary{iPhenoStage,'Data'}.adVO2W));
        ShapeTable.Table.Cell(iTableRow,4).Shape.TextFrame.TextRange.Text=sTempVolRat;
        ShapeTable.Table.Cell(iTableRow,5).Shape.TextFrame.TextRange.Text=sprintf('%1.0f',mean(dsSummary{iPhenoStage,'Data'}.adVW));
        ShapeTable.Table.Cell(iTableRow,6).Shape.TextFrame.TextRange.Text=sTempVol;
        ShapeTable.Table.Cell(iTableRow,7).Shape.TextFrame.TextRange.Text=sprintf('%1.3f',mean(dsSummary{iPhenoStage,'Data'}.adIO2W));
        ShapeTable.Table.Cell(iTableRow,8).Shape.TextFrame.TextRange.Text=sTempInt;
        ShapeTable.Table.Cell(iTableRow,9).Shape.TextFrame.TextRange.Text=sprintf('%d',length(dsSummary{iPhenoStage,'Data'}.adIO2W));
        iTableRow=iTableRow+1;
        
    end;
    
    %Make Excel visible
    apExcel.Worksheets.Item(1).Activate;
    apExcel.visible = true;
    

    for iData = 1:length(dsData)
        NewSlide = Presentation.Slides.invoke('Add',Presentation.Slides.Count+1,11);
        NewSlide.Shapes.Title.TextFrame.TextRange.Text=[strtrim(dsData{iData,'Genotype'}) ', Stage ' dsData{iData,'Stage'}];
        
        ShapeTable = NewSlide.Shapes.AddTable(7,2);
        %Make the table bigger
        ShapeTable.Left  = 20;
        ShapeTable.Width = Presentation.PageSetup.SlideWidth/2-30; 
        %Set table header
        ShapeTable.Table.Cell(1,1).Shape.TextFrame.TextRange.Text='Genotype';
        ShapeTable.Table.Cell(1,2).Shape.TextFrame.TextRange.Text=strtrim(dsData{iData,'Genotype'});
        ShapeTable.Table.Cell(2,1).Shape.TextFrame.TextRange.Text='Stage';
        ShapeTable.Table.Cell(2,2).Shape.TextFrame.TextRange.Text=strtrim(dsData{iData,'Stage'});
        ShapeTable.Table.Cell(3,1).Shape.TextFrame.TextRange.Text='Volume Ooc/whole';
        ShapeTable.Table.Cell(3,2).Shape.TextFrame.TextRange.Text=num2str(dsData{iData,'VolOocToWhole'});
        ShapeTable.Table.Cell(4,1).Shape.TextFrame.TextRange.Text='Total volume';
        ShapeTable.Table.Cell(4,2).Shape.TextFrame.TextRange.Text=num2str(dsData{iData,'VolOoc'}+dsData{iData,'VolNur'},'%1.0f');
        ShapeTable.Table.Cell(5,1).Shape.TextFrame.TextRange.Text='Intensity Ooc/whole';
        ShapeTable.Table.Cell(5,2).Shape.TextFrame.TextRange.Text=num2str(dsData{iData,'IntOocToWhole'});
        ShapeTable.Table.Cell(6,2).Merge(ShapeTable.Table.Cell(6,1));
        ShapeTable.Table.Cell(6,1).Shape.TextFrame.TextRange.Text=dsData{iData,'Histogram'}.file;
        ShapeTable.Table.Cell(7,1).Shape.TextFrame.TextRange.Text='Comment:';
        %Add the comment, just pay attention, that it should be a string
        sComment=dsData{iData,'Histogram'}.comment;
        if(~ischar(sComment))
            sComment='';
        end;
        ShapeTable.Table.Cell(7,2).Shape.TextFrame.TextRange.Text=sComment;
        
        %Try to import image into Powerpoint
        try
            aiImage=imread(dsData{iData,'Histogram'}.thumb);
            aiImage=uint8(512/double(max(max(aiImage)))*aiImage);
            imwrite(aiImage,sTempImage,'jpeg');
            %Determine whether it will fit on the slide
            dWidth = Presentation.PageSetup.SlideWidth/2-10;
            dHeight = dWidth*size(aiImage,1)/size(aiImage,2);
            if (dHeight>Presentation.PageSetup.SlideHeight-100)
                dHeight = Presentation.PageSetup.SlideHeight-100;
                dWidth  = dHeight * size(aiImage,2)/size(aiImage,1);
            end;
            NewSlide.Shapes.AddPicture(sTempImage,false, true, Presentation.PageSetup.SlideWidth/2,100, dWidth ,dHeight );
        catch error
            disp(error.message);
            disp(['Couldn''t add image ' dsData{iData,'Histogram'}.thumb]);
        end;
        
        %Add histogram
        NewSlide = Presentation.Slides.invoke('Add',Presentation.Slides.Count+1,11);
        NewSlide.Shapes.Title.TextFrame.TextRange.Text=[strtrim(dsData{iData,'Genotype'}) ', Stage ' dsData{iData,'Stage'} char(10) 'Histogram for Intensity'];
        
        %Plot data
        figure('Visible','Off')
        plot(cell2mat(dsData{iData,'Histogram'}.data(:,1)),cell2mat(dsData{iData,'Histogram'}.data(:,2:3)));
        %Set windows size
        set(gcf,'Position',[0,500,3*(Presentation.PageSetup.SlideWidth-20), 3*(Presentation.PageSetup.SlideHeight-100)]);
        %Set limits
        set(gca,'XLim',[1000, 15000]);
        %Make a legend
        legend('Oocyte','Nurse cells');
        %Save file and add to new slide
        saveas(gcf,sTempImage,'jpeg');
        NewSlide.Shapes.AddPicture(sTempImage,false, true, 10,100, Presentation.PageSetup.SlideWidth-20, Presentation.PageSetup.SlideHeight-100 );
        %Close figure again
        close;
        
    end;
    delete(sTempImage);
    %Make Powerpoint visible
    apPPT.visible=true;
end

